<template>
    <section>
        <!--工具条-->

        <div class="section-content clearfix">
            <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
                <el-form :inline="true" :model="filters">
                    <el-form-item style="float: left;">
                        <el-input v-model="filters.name" placeholder="模板名称" @keyup.enter.native="getUsers()"></el-input>
                    </el-form-item>
                    <el-form-item class="tool-bt">
                        <el-button :type="buttonColor" v-on:click="getUsers">查询</el-button>
                    </el-form-item>
                    <el-form-item >                     
                        <el-button :type="buttonColor" @click="handleAdd">新增</el-button>
                    </el-form-item>
                </el-form>
            </el-col>

            <!--列表-->
            <el-table :data="query" highlight-current-row style="width: 100%;" :header-row-style="{backgroundColor: titleColor}">
                <!-- <el-table-column type="selection">
                </el-table-column> -->
                <el-table-column type="index" align="center" label="序号"  width="80":index="indexMethod">
                </el-table-column>
                <el-table-column align="center" label="模板名称">
                    <template scope="scope">
                        <span class="pb-name" @click="getrows(scope.$index, scope.row)">{{scope.row.name}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="updateTime" align="center" label="更新时间">
                </el-table-column>
                <el-table-column align="center" label="操作">
                    <template scope="scope">
                        <i title="编辑" @click="handleEdit(scope.$index, scope.row)" icon="el-icon-edit" circle class="pb-icon icon-edit"></i>
                        <i title="删除" @click="handleDel(scope.$index, scope.row)" icon="el-icon-delete" circle class="pb-icon icon-delete"></i>
                        <span title="组织授权" style="color:#0099cc;vertical-align: top;cursor: pointer;" @click="goCompanyUI(scope.$index, scope.row)">授权</span>
                    </template>
                </el-table-column>
            </el-table>

            <!--工具条-->
            <el-col :span="24" class="toolbar">
                <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total"
                    style="float:right;">
                </el-pagination>
            </el-col>
        </div>
        <!--新增界面-->
        <el-dialog title="新增" v-show="addFormVisible" v-model="addFormVisible" @close="addFormVisible=false" :custom-class="dialogTitleColor">
            <div slot="footer" style="text-align: left; ">
                <el-form @submit.native.prevent :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
                    <el-form-item label="模板名称" prop="name">
                        <el-input v-model="addForm.name" auto-complete="off"></el-input>
                    </el-form-item>      
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align: right;">
                    <el-button @click.native="addFormVisible = false">取消</el-button>
                    <el-button :type="buttonColor" @click.native="addSubmit" :loading="addLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>
        <!--编辑界面-->
        <el-dialog title="编辑" v-show="editFormVisible" v-model="editFormVisible" @close="editFormVisible=false" :custom-class="dialogTitleColor">
            <div slot="footer" style="text-align: left; ">
                <el-form @submit.native.prevent :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="editForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align: right;">
                    <el-button @click.native="editFormVisible = false">取消</el-button>
                    <el-button :type="buttonColor" @click.native="editSubmit" :loading="addLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>
    </section>
</template>

<script>
import {
    template_ui_query,
    template_ui_post,
    template_ui_put,
    template_ui_get,
    template_ui_delete   
} from "../../api/api"
export default {
    data(){
        return{
            page:"1",
            total: 0,
            addFormVisible: false,
            editFormVisible: false,
            addLoading:false,
            filters:{
                name:''
            },
            query:[],
            addForm:{
                name: '',
                cmdType:'',
                remark: ''
            },
            editForm:{
                name: '',
                cmdType:'',
                remark: ''
            },
            addFormRules:{
                name: [{
                        required: true,
                        message: '请输入名字',
                        trigger: 'blur'
                    },  { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }],
            },
            editFormRules:{
                name: [{
                        required: true,
                        message: '请输入名字',
                        trigger: 'blur'
                    },  { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }],
            }
        }
    },
    computed:{
            buttonColor(){
                return this.$store.state.buttonColor
            },
            titleColor(){
                return this.$store.state.titleColor
            },
            dialogTitleColor(){
                return this.$store.state.dialogTitleColor
            },
        },
    methods:{
        getrows:function(index,row){
                sessionStorage.setItem('uitemplateId',row.uiId)
                this.$router.push("/template/uicmd"); 
            },
        getUsers(){
            let para = {
                // pageNo: this.page,
                pageSize: 10,
                name: this.filters.name
            }
            template_ui_query(para).then(res => {
                console.log(res)
                if(res.code == 0){
                    this.query = res.data.list;
                    this.total = res.data.totalCount;
                }
            })
        },
        indexMethod(index) {
                let num = (this.page - 1) * 10 + index + 1;
                return num;
            },
        handleCurrentChange(val) {
                this.page = val;
                this.getUsers();
            },
        //显示新增界面
        handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    cmdType:'',
                    remark: ''
                };
            },
        //新增
        addSubmit: function () {
            this.$refs.addForm.validate((valid) => {
                if (valid) {
                    this.$confirm('确认提交吗？', '提示', {}).then(() => {
                    this.addLoading = true;
                        //NProgress.start();
                    let para = Object.assign({}, this.addForm);
                    console.log(para)
                    template_ui_post(para).then((res) => {
                            this.begCode(res.code,res.message);
                            this.addLoading = false;
                            //NProgress.done();
                            
                    if (res.code == 0) {
                            this.$message({
                                message: '提交成功',
                                type: 'success'
                            });
                            this.$refs['addForm'].resetFields();
                            this.addFormVisible = false;
                            this.getUsers();
                        }
                        });
                        
                    });
                }
            });
        },
        //显示编辑界面
            handleEdit: function (index, row) {
                console.log(row)
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
            },
        //编辑
        editSubmit: function () {
            this.$refs.editForm.validate((valid) => {
                if (valid) {
                    this.$confirm('确认提交吗？', '提示', {}).then(() => {
                        this.editLoading = true;
                        let para = Object.assign({}, this.editForm);
                        // console.log(para)
                        template_ui_put(para).then((res) => {
                            this.begCode(res.code,res.message);
                            this.editLoading = false;
                            if (res.code == 0) {
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.getUsers();
                            }
                        });
                    });
                }
            });
        },
        //删除
        handleDel: function(index, row){
        this.$confirm('确认删除吗?', '提示', {
                type: 'warning'
            }).then(() => {
                this.listLoading = true;
                //NProgress.start();
                let para = {
                    uiId: row.uiId
                };
                template_ui_delete(para).then((res) => {
                        this.begCode(res.code,res.message);
                    if (res.code == 0) {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getUsers();
                    }
                });
            }).catch(() => {

            });
        },
        goCompanyUI(index,row){
            this.$router.push({
                path:'/company/companyUI'
            })
            sessionStorage.setItem('companyuiName',row.name)
            sessionStorage.setItem('companyuiId',row.uiId)
        }
    },
    mounted(){
        this.getUsers()
    }
}
</script>

<style scoped>

</style>